/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:42:12  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.risk.controller;

import javax.validation.Valid;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.risk.entity.RiskDistrictEvaluation;
import com.joysuch.wwyt.risk.service.RiskDistrictEvaluationService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "双预防-风险评估")
@RequestMapping("/risk/district-evaluation")
public class RiskDistrictEvaluationController {

    private static final Logger logger = LoggerFactory.getLogger(RiskDistrictEvaluationController.class);

    @Autowired
    private RiskDistrictEvaluationService riskDistrictEvaluationService;

    @RequestMapping("add")
    public Object add(@Valid RiskDistrictEvaluation entity) {
        try {
            riskDistrictEvaluationService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @RequestMapping("update")
    public Object update(@Valid RiskDistrictEvaluation entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            riskDistrictEvaluationService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page) {
        try {
            Page<RiskDistrictEvaluation> data = riskDistrictEvaluationService.findByPage(page);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            riskDistrictEvaluationService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }
}
